import store from '@/store'

function checkPermission(el, binding) {
  const value = binding.arg
  const myMenuPrivilege = store.getters && store.getters.myMenuPrivilege
  if (myMenuPrivilege && myMenuPrivilege.length === 1 && myMenuPrivilege[0] === "all") {
    console.log("超级管理员指令权限")
    return true;
  } else {
    if (!value) {
      console.log("按钮没有配置指令权限", value)
      return true;
    } else {
      if (myMenuPrivilege) {
        console.log("按钮指令权限", value)
        const pkey = value
        const hasPermission = myMenuPrivilege.some(p => {
          return pkey === p;
        })
        if (!hasPermission) {
          el.parentNode && el.parentNode.removeChild(el)
        }
      } else {
        throw new Error(`没有权限访问>${value}`)
      }
    }
  }
}

export default {
  inserted(el, binding) {
    checkPermission(el, binding)
  },
  update(el, binding) {
    checkPermission(el, binding)
  }
}
